﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.MessagingManage
{
    public partial class NoteSentList : Common.PageBase
    {
        public string ModifyUrl = "NoteShow.aspx?AUTOID={0}";
        T_NOTE_MSG_BLL bll = new T_NOTE_MSG_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 定义数据的页面自定义变量
        string _AUTOID = String.Empty;
        string _SUBJECT = String.Empty;
        string _CONTENT = String.Empty;
        string _STATUS = String.Empty;
        string _RECVID = String.Empty;
        string _SENDID = String.Empty;
        string _RECVNAME = String.Empty;
        string _SENDNAME = String.Empty;
        string _CREATETIMEBegin = String.Empty;
        string _CREATETIMEEnd = String.Empty;
        string _FILES = String.Empty;
        string _FILEPATH = String.Empty;
        string _SEND_BUSINESSDIVISIONID = String.Empty;
        string _SEND_BUSINESSDIVISIONNAME = String.Empty;
        string _SEND_BUSINESSDIVISIONCODE = String.Empty;
        string _RECV_BUSINESSDIVISIONID = String.Empty;
        string _RECV_BUSINESSDIVISIONNAME = String.Empty;
        string _RECV_BUSINESSDIVISIONCODE = String.Empty;
        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(_AUTOID))
            {
                sql.Append(" and AUTOID = " + _AUTOID + "");
            }
            if (!String.IsNullOrEmpty(_SUBJECT))
            {
                sql.Append(" and SUBJECT like '%" + _SUBJECT + "%'");
            }
            if (!String.IsNullOrEmpty(_CONTENT))
            {
                sql.Append(" and CONTENT like '%" + _CONTENT + "%'");
            }
            if (!String.IsNullOrEmpty(_STATUS))
            {
                sql.Append(" and STATUS like '%" + _STATUS + "%'");
            }
            if (!String.IsNullOrEmpty(_RECVID))
            {
                sql.Append(" and RECVID = '" + _RECVID + "'");
            }
            if (!String.IsNullOrEmpty(_SENDID))
            {
                sql.Append(" and SENDID = '" + _SENDID + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RECVNAME"]))
            {
                sql.Append(" and RECVNAME like '%" + _RECVNAME + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SENDNAME"]))
            {
                sql.Append(" and SENDNAME like '%" + _SENDNAME + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(_FILES))
            {
                sql.Append(" and FILES like '%" + _FILES + "%'");
            }
            if (!String.IsNullOrEmpty(_FILEPATH))
            {
                sql.Append(" and FILEPATH like '%" + _FILEPATH + "%'");
            }

            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and SEND_BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }

            //if (!String.IsNullOrEmpty(_SEND_BUSINESSDIVISIONID))
            //{
            //    sql.Append(" and SEND_BUSINESSDIVISIONID = '" + _SEND_BUSINESSDIVISIONID + "'");
            //}
            //if (!String.IsNullOrEmpty(_SEND_BUSINESSDIVISIONNAME))
            //{
            //    sql.Append(" and SEND_BUSINESSDIVISIONNAME like '%" + _SEND_BUSINESSDIVISIONNAME + "%'");
            //}
            //if (!String.IsNullOrEmpty(_SEND_BUSINESSDIVISIONCODE))
            //{
            //    sql.Append(" and SEND_BUSINESSDIVISIONCODE like '%" + _SEND_BUSINESSDIVISIONCODE + "%'");
            //}
            //if (!String.IsNullOrEmpty(_RECV_BUSINESSDIVISIONID))
            //{
            //    sql.Append(" and RECV_BUSINESSDIVISIONID = '" + _RECV_BUSINESSDIVISIONID + "'");
            //}
            //if (!String.IsNullOrEmpty(_RECV_BUSINESSDIVISIONNAME))
            //{
            //    sql.Append(" and RECV_BUSINESSDIVISIONNAME like '%" + _RECV_BUSINESSDIVISIONNAME + "%'");
            //}
            //if (!String.IsNullOrEmpty(_RECV_BUSINESSDIVISIONCODE))
            //{
            //    sql.Append(" and RECV_BUSINESSDIVISIONCODE like '%" + _RECV_BUSINESSDIVISIONCODE + "%'");
            //}

            return sql;
        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_NOTE_MSG_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "AUTOID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_NOTE_MSG_Entity entity = new T_NOTE_MSG_Entity();
            entity.AUTOID= StringHelper.ToInt((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {
            }
        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    default:
                                        {
                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {
                    }
                    break;
                case DataControlRowType.Pager:
                    {
                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {
                    }
                    break;
                default:

                    break;
            }
        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
